package com.lrz.controller;

import com.lrz.common.Result;
import com.lrz.common.StatusCode;
import com.lrz.pojo.Article;
import com.lrz.pojo.dto.ArticleDTO;
import com.lrz.service.ArticleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @GetMapping
    public Result findAll(){
        List<ArticleDTO> list = articleService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    @GetMapping("/user/{userId}")
    public Result findByUserId(@PathVariable Long userId){
        List<Article> list = articleService.findByUserId(userId);
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Long id){
        Long userId = Long.valueOf(1);
        ArticleDTO article = articleService.findById(userId, id);
        return new Result(true, StatusCode.OK, "查询成功", article);
    }


    @GetMapping("/state")
    public Result findByState(){
        List<Article> list = articleService.findByState();
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Article article){
        System.out.println(article);
        articleService.add(article);
        return new Result(true, StatusCode.OK, "添加成功，等待管理员审核之后发布");
    }

    @PostMapping("/update")
    public Result update(@RequestBody Article article){
        articleService.update(article);
        return new Result(true, StatusCode.OK, "修改成功，等待管理员审核之后发布");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        articleService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

}
